MY_CFLAGS=-Og -g -DDEBUG
# append -DDEBUG to enable logging
ifneq ($(KERNELRELEASE),)
	obj-m := jit.o
	jit-objs := jit_module.o current_time.o busy.o schedule.o queue.o \
				timer.o tasklet.o workqueue.o
	ccflags-y += $(MY_CFLAGS)
else
	#KERNELDIR ?= /lib/modules/$(shell uname -r)/build
	KERNELDIR ?= /home/sam/sandbox/linux
	PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules EXTRA_CFLAGS="$(MY_CFLAGS)"
endif

jit_module.o: jit_module.c log.h
current_time.o: current_time.c log.h
busy.o: busy.c log.h
schedule.o: schedule.c log.h
queue.o: queue.c log.h
timer.o: timer.c log.h
tasklet.o: tasklet.c log.h
workqueue.o: workqueue.c log.h

run: default
	qemu-system-x86_64 -enable-kvm -cpu host -smp 4 \
	-m 256M \
	-nographic \
	-kernel ../../linux/arch/x86_64/boot/bzImage \
	-initrd ../initramfs.tgz \
	-fsdev local,id=ldd,path=$(PWD),security_model=mapped,multidevs=remap \
	-device virtio-9p-pci,fsdev=ldd,mount_tag=ldd \
	-append "nokaslr console=ttyS0 vm_inst=1 kgdbwait kgdboc=ttyS1,115200"

# After running, you can mount the fsdev
# mkdir -p /lib/modules/`uname -r`
# mount -t 9p -o trans=virtio ldd /lib/modules/`uname -r`

debug: default
	qemu-system-x86_64 -enable-kvm -cpu host -smp 1 \
	-m 256M \
	-nographic \
	-kernel ../../linux/arch/x86_64/boot/bzImage \
	-initrd ../initramfs.tgz \
	-fsdev local,id=ldd,path=$(PWD),security_model=mapped,multidevs=remap \
	-device virtio-9p-pci,fsdev=ldd,mount_tag=ldd \
	-append "nokaslr console=ttyS0 vm_inst=1" \
	-s -S

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules clean

.PHONY: default run debug clean
#	-chardev socket,id=gdb,port=1234,server,nowait,host=0.0.0.0 \
#	-device pci-serial,chardev=gdb \
#	-append "nokaslr console=ttyS0 vm_inst=1 kgdbwait kgdboc=ttyS1,115200"
